﻿<html>
	<body background="pic/background_06.gif">
	<head>
		<font face="新細明體,time new roman,Arial">
		<meta charset="UTF-8">
	</head>
	<body>
		<center>
			<font size="5" color="#0000FF"><b>--- 下載程式/文件,查詢結果 ---</b></font><br>
			<hr>
			<?php
				// Loading all php class variable and fucntions ...
				require_once('class_package/common.php');
				
				// 各下载程式, 输入资料判断 ...
				$get_prgm_select = $_POST['prgm_select'];		
				$append_InputFailStr[] = '';
				$webdl_work_order = "";
				$ict_show_all = 0;
				
				switch ( $get_prgm_select ) {
					case 'OQC-F程式':
						$get_pn			= strtoupper($_POST['webdl_pn']);	
						$webdl_wsid   	=	"OQC-F";
						$webdl_sn		=	"N/A";
						$webdl_pn=$get_pn;
						break;				
					default:
						echo "<script>alert('查无此程式下载的选项!!');history.go(-1);</script>";
						exit();
						break;
				}
				// 输入查询资料各栏位中, 不能为空值
				// 输入查询资料-Part Number(局部料号), 应该为 13 字元
				// 输入错误判断, 提示讯息; php执行完alert之后, 返回上一页 ...
				if ( count($append_InputFailStr) > 1 ) {
					echo "<script>alert('輸入的查詢欄位中, 不能為空值, 請重新輸入!!');history.go(-1);</script>";
					exit();
				}
				if ( $get_prgm_select == 'OQC-F程式' ) {
					if ( strlen($webdl_pn) != 13 ) {
						echo "<script>alert('輸入的查詢欄位中-Part Number(局部料號), 應該為 13 個字元, 請重新輸入!!');history.go(-1);</script>";
						exit();
					}
				}
				
				// To search all table ...
				$db_select   = Sql_Download::sql_db;
				$db_table    = Sql_Download::sql_table;
				$table_name1 = Sql_Download::table_name1;	//webdl_pn
				$table_name2 = Sql_Download::table_name2;	//webdl_wsid
				$table_name3 = Sql_Download::table_name3;	//webdl_tpver
				$table_name4 = Sql_Download::table_name4;	//webdl_work_order
				$table_name5 = Sql_Download::table_name5;	//webdl_line
				$table_name6 = Sql_Download::table_name6;	//webdl_panel
				$conn = '';
				$sql_cmd = '';
				$rows = 0;
				$result = '';
				$prg_rows_fail = 0;
				$sop_rows_fail = 0;
				
				// Check connection for show program...
				$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
				if ( mysqli_connect_errno($conn) ) { 
					echo "<script>alert('MySql資料庫連接失敗,請聯系開發人員,並查明原因...');history.go(-1);</script>";
					exit();
				}
				mysqli_set_charset($conn,'set names utf8');
				
				switch ( $get_prgm_select ) {
					case "OQC-F程式":
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
						break;
					default:
						echo "<script>alert('查無此程式下載的選項!!');history.go(-1);</script>";
						exit();
						break;
				}
				
				$result = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
				if ( !$result ) { 
					mysqli_close($conn);				// 关闭资料库
					echo "<script>alert('MySql資料庫搜尋失敗, 請跟開發人員查明原因...');history.go(-1);</script>";
					exit();
				}
				
				$rows = mysqli_num_rows($result);		// 返回结果集中栏位的数量。	
			
				if ( $rows == 0 ) { 
					$prg_rows_fail = 1;					
				}else{
					switch ( $get_prgm_select ) {
						case 'OQC-F程式':
					
							echo '<br>
							<font size="4" color="#000000"><b>OQC-F程式:</b></font>
							<table border="2" width="75%">
								<tr bgcolor="#70DB93">
									<th>No.</th>
									<th>工單</th>
									<th>Serial Number</th>
									<th>Part Number</th>
									<th>WSID</th>
									<th>TPVER</th>
									<th>线别</th>
									<th>制程别</th>
									<th>Create Date/Time</th>
									<th>Create By</th>
									<th>Description(位址/ChkSum)</th>
									<th>Download</th>
								</tr>';
							break;
						
						default:
							echo "<script>alert('查無此程式下載的選項!!');history.go(-1);</script>";
							exit();
							break;
					}
					
					$show_num = 0;
					while ( $getArray = mysqli_fetch_array($result,MYSQLI_ASSOC) ) {
						// To loading all datas from MySQL ...
						$show_num++;
						$put_pn          = $getArray['webdl_pn'];
						$put_wsid        = $getArray['webdl_wsid'];
						$put_tpver       = $getArray['webdl_tpver'];
						$put_work_order  = $getArray['webdl_work_order'];
						$put_line        = $getArray['webdl_line'];
						$put_panel       = $getArray['webdl_panel'];
						$put_create_date = $getArray['webdl_create_date'];
						$put_creator     = $getArray['webdl_creator'];
						$put_description = $getArray['webdl_description'];
						$put_ftpip       = $getArray['webdl_ftpip'];
						$put_count       = $getArray['webdl_count'];
						$download_ftpip  = "webdl_download_count.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip&webdl_count=$put_count&creator=$put_creator";
						// $download_ftpip  = "webdl_download_count.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip&webdl_count=$put_count";
						echo "
							<tr>
								<th>$show_num</th>
								<th><font size='4' color='#0000FF'><b>$put_work_order</b></font></th>
								<th>$webdl_sn</th>
								<th>$put_pn</th>
								<th>$put_wsid</th>
								<th>$put_tpver</th>
								<th>$put_line</th>
								<th>$put_panel</th>
								<th>$put_create_date</th>
								<th>$put_creator</th>
								<th>$put_description</th>
								<th><a href=$download_ftpip>下 载</a></th>					
							</tr>";
					}
					echo '</table><br>';
					
					
				
				}
					mysqli_free_result($result);			// 释放 MySQL $result
					mysqli_close($conn);					// 关闭资料库
				
				
				if ( $prg_rows_fail == 1 ) {
					echo "<script>alert('查無此筆上傳程式的資料, 請通知相關負責人建...');history.go(-1);</script>";
				} 
				/*elseif ( $prg_rows_fail == 1 && $sop_rows_fail == 1 ) {
					echo "<script>alert('查無此筆上傳程式及SOP的資料, 請通知相關負責人建立...');history.go(-1);</script>";
				}*/
				exit();
			?>
		</center>
	</body>
</html>
